------------------ fgui require start ------------------
------------------  fgui require end  ------------------
local GuildApplySkin = Class("GuildApplySkin");
local Listdetails2 = require("View/UIGuild/Skin/Listdetails2Skin")

function GuildApplySkin:ctor(com)
    ------------------ fgui事件绑定start ------------------
    self:bindChild(com);
    self.ctl_hasApply.onChanged:Add(self.onChange_Ctl_hasApply,self);
    self.btn_blackbackground.onClick:Add(self.onTouch_Btn_blackbackground,self);
    self.btn_cancel.onClick:Add(self.onTouch_Btn_cancel,self);
    self.btn_determine.onClick:Add(self.onTouch_Btn_determine,self);
    self.list_apply.onClickItem:Add(self.onClickItem_List_apply,self);
    self.list_apply.itemRenderer = function(index,obj) self:onRender_List_applyItem(index,obj); end;
    self:init();
    ------------------  fgui事件绑定end  ------------------
end

function GuildApplySkin:bindChild(com)
    ------------------ fgui变量绑定start ------------------
    self.fguiObj = com;
    self.ctl_hasApply = com:GetController("Ctl_hasApply");
    self.btn_blackbackground = com:GetChild("Btn_blackbackground");
    self.btn_cancel = com:GetChild("Btn_cancel");
    self.btn_determine = com:GetChild("Btn_determine");
    self.text_close = com:GetChild("Text_close");
    self.text_title = com:GetChild("Text_title");
    self.text_title.text = getLanguage("GuildInvestment_1059");
    self.text_numberofguildmembers = com:GetChild("Text_numberofguildmembers");
    self.list_apply = com:GetChild("List_apply");
    ------------------  fgui变量绑定end  ------------------
end

function GuildApplySkin:init()
    self.listItems = {}
    self.askJoinList = logicMgr.GuildManager.unionInfo.askJoinList;
    self.totalAskCnt = #self.askJoinList;
    ListenEvent(Config.EventType.Union_JoinList_S2C, self.respApplyUnionJoin,self);
end

function GuildApplySkin:OnDestroy()
    ------------------ fgui事件移除start ------------------
    self:beforeDestroy();
    self.ctl_hasApply.onChanged:Clear();
    self.btn_blackbackground.onClick:Clear();
    self.btn_cancel.onClick:Clear();
    self.btn_determine.onClick:Clear();
    self.list_apply.onClickItem:Clear();
    self.list_apply.itemRenderer = nil;
    ------------------  fgui事件移除end  ------------------
end

function GuildApplySkin:onTouch_Btn_blackbackground(obj) end

function GuildApplySkin:onTouch_Btn_cancel(obj)
    local unionId = logicMgr.GuildManager.getUnionId();
    local isArgee = 0;
    local ids = self:getApplayIds();
    unionHandler.reqHandleJoinList(unionId,isArgee,ids);
    --closeUI(PanelNames.UIGuildPanel)
    --self.btn_blackbackground:FireClick(false)
    if self.closeCb~=nil then
        self.closeCb()
    end
end

function GuildApplySkin:onTouch_Btn_determine(obj)
    local ids = self:getApplayIds();
    --if #ids == 0 then
    --    UISysTips.AddMsg(getLanguage("GuildInvestment_1151"));
    --    return;
    --end
    local unionId = logicMgr.GuildManager.getUnionId();
    local isArgee = 1;
    unionHandler.reqHandleJoinList(unionId,isArgee,ids);
    --closeUI(PanelNames.UIGuildPanel)
    --self.btn_blackbackground:FireClick(false)

    if self.closeCb~=nil then
        self.closeCb()
    end
end

function GuildApplySkin:onClickItem_List_apply(context) 
    local item = self.listItems[context.data];
    if  tostring(logicMgr.HeroManager.GetRoleInfo().playerId) == tostring(item:getPlayerId()) then
        -- showUI(PanelNames.UIHeadInfo);
    else
        roleInfoHandler.sendreqOtherRoleInfo(item:getPlayerId());
    end
end

function GuildApplySkin:onRender_List_applyItem(index,obj)
    local item = self.listItems[obj];
    if item == nil then
        item = Listdetails2.New(obj);
        self.listItems[obj] = item;
    end
    item:onRender(logicMgr.GuildManager.unionInfo.askJoinList[index+1]);
end

function GuildApplySkin:onChange_Ctl_hasApply(event) end

------------------  ↑ fgui事件句柄 ↑  ------------------

function GuildApplySkin:setCloseCallback(func,target)
    self.btn_blackbackground.onClick:Add(func,target);
    self.closeCb=function()
        func(target);
    end
end

function GuildApplySkin:updateView()
    local itemCnt = #logicMgr.GuildManager.unionInfo.askJoinList;
    self.text_numberofguildmembers.text =  string.format(getLanguage("GuildInvestment_1069"), itemCnt , self.totalAskCnt);
    self.list_apply.numItems = itemCnt;
    self.ctl_hasApply.selectedIndex = itemCnt > 0 and 1 or 0;
end

function GuildApplySkin:respApplyUnionJoin(msg)
    self:updateView();
end

--获取可以申请的ids
function GuildApplySkin:getCanApplayIds()
    local remainCnt = math.min(logicMgr.GuildManager.getRemainApplyCnt(),self.list_apply.numItems);
    local ids = {}
    for i = 0, remainCnt-1 do
        local obj = self.list_apply:GetChildAt(i);
        table.insert(ids,self.listItems[obj].data.showInfo.playerId);
    end
    return ids;
end

function GuildApplySkin:getApplayIds()
    local remainCnt = self.list_apply.numItems
    local ids = {}
    for i = 0, remainCnt-1 do
        local obj = self.list_apply:GetChildAt(i);
        table.insert(ids,self.listItems[obj].data.showInfo.playerId);
    end
    return ids;
end

function GuildApplySkin:beforeDestroy()
    self.listItems = {}
    CloseEvent(Config.EventType.Union_JoinList_S2C, self.respApplyUnionJoin,self);
end

return GuildApplySkin;
